import 'package:flutter/material.dart';
import 'package:sow_app/view/common/novel/novel_list_tile.dart';

import 'package:sow_app_core/sow_app_core.dart';
import 'package:sow_app_i18n/sow_app_i18n.dart';

import '../../presenter/state_home_presenter.dart';

class HomePage extends StatefulWidget implements AppPage {
  const HomePage({Key? key, required this.controller, required this.presenter})
      : super(key: key);
  final HomeController controller;
  final StateHomePresenter presenter;

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  initState() {
    widget.presenter.presenterContext = context;
    widget.presenter.presenterSetState = setState;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: false,
        title: const Text(I18n.welcome),
      ),
      body: DefaultTabController(
        length: widget.presenter.tabs.length,
        child: Column(
          children: [
            Align(
              alignment: Alignment.centerLeft,
              child: TabBar(
                  isScrollable: true,
                  tabs: widget.presenter.tabs,
                  onTap: (value) {
                    widget.controller.init();
                  }),
            ),
            Expanded(
              child: TabBarView(
                children: [
                  ListView(
                    children: widget.presenter.bookshelf
                        .map((e) => NovelListTile(
                              novel: e,
                              onUpdate: widget.controller.init,
                            ))
                        .toList(),
                  ),
                  ListView(
                    children: widget.presenter.history
                        .map((e) => NovelListTile(
                            novel: e, onUpdate: widget.controller.init))
                        .toList(),
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}
